qinglan.Canvas.Loop = qinglan.Canvas.Class(function(callback,context){
	this.callback = callback;
	this.context = context;

	this.isLoop = true;
	this.lastLoopTime = 0;

	this.doLoop();
}).methods({
	doLoop: function(){
		
		var self = this;
		var loop = function(){
			if (!self.isLoop) { return false;}
			requestAnimationFrame(loop);
			var now = new Date();
			qinglan.Type.isFunction(self.callback) && self.callback.call(self.context,now - self.lastLoopTime);
			self.lastLoopTime = now;
		};
		loop();
	},
	stop: function(){
		this.isLoop = false;
	},
	resume: function(){
		this.isLoop = true;
		this.lastLoopTime = new Date();
		this.doLoop();
	}
});
